package com.lmxdawn.api.dnf.service.impl;

import com.lmxdawn.api.common.session.UserSession;
import com.lmxdawn.api.dnf.service.IAuthorization;
import com.lmxdawn.api.dnf.service.IWebLoginService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限校验
 * <p>
 * 1、权限放入redis缓存中
 *
 * @author jy
 * @since 2024-04-07 10:01
 */
@Component
public class AuthorizationImpl implements IAuthorization {

    @Resource
    private IWebLoginService webLoginService;

    @Override
    public boolean isPermitted(UserSession userSession, String permission) {
        if (StringUtils.isEmpty(permission)) {
            return false;
        }
        // 获取用户拥有的菜单权限
        List<String> permissionCacheList = webLoginService.getPermissionFromCache(userSession.getId());
        if (ObjectUtils.isEmpty(permissionCacheList)) {
            return false;
        }
        // 分割权限
        String[] permissionList = StringUtils.split(permission, ";");
        for (String value : permissionList) {
            if (permissionCacheList.contains(value)) {
                return true;
            }
        }
        return false;
    }

}
